package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.utils.DateUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {
    @Resource
    private OrderDao orderDao;
    @Resource
    private MemberDao memberDao;
    @Resource
    private OrderSettingDao orderSettingDao;
    @Override
    public Result order(Map map) throws Exception{
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(date);
        if (orderSetting==null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations>=number){
            //预约已满，不能预约
            return new Result(false,MessageConstant.ORDER_FULL);
        }
        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        String telephone = (String) map.get("telephone");//获取用户界面输入的手机号
        //根据电话查询是否为会员
        Member member = memberDao.findByTelephone(telephone);
        if (member!=null){
            //是会员，判断是否重复预约
            Integer memberId = member.getId();//会员id
            Date order_date = DateUtils.parseString2Date(orderDate);//用户输入的预约日期
            Integer setmealId = Integer.parseInt((String) map.get("setmealId"));//套餐id
            //封装成体检信息实体类
            Order order = new Order(memberId,order_date,setmealId);
            //根据条件查询是否有此订单
            List<Order> orderList = orderDao.findByCondition(order);
            if (orderList!=null&&orderList.size()>0){
                //说明用户在重复预约，无法再次预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {
            //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
            //当前用户不是会员，需要添加到会员表
            member = new Member();
            member.setName((String) map.get("name"));//设置姓名
            member.setPhoneNumber(telephone);//设置手机号
            member.setIdCard((String) map.get("idCard"));//设置身份证号
            member.setSex((String) map.get("sex"));//设置性别
            member.setRegTime(new Date());//设置创建时间
            memberDao.add(member);//自动注册会员，返回主键值（会员id）
        }

        //5、预约成功，更新当日的已预约人数
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员id
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐id
        orderDao.add(order);

        //可以预约，设置预约人数加一
        orderSetting.setReservations(orderSetting.getReservations()+1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if (map!=null){
            Date orderDate = (Date) map.get("orderDate");
            //处理日期格式
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

    @Override
    public List<String> findOrderDate() {
        List<String> list  = orderDao.findOrderDate();
        return list;
    }

    @Override
    public Integer findOrderCount(String date) {
        Integer count = orderDao.findOrderCount(date);
        return count;
    }

    @Override
    public Integer findOrderStatusCount(String date) {
        Integer count = orderDao.findOrderStatusCount(date);
        return count;
    }



    @Override
    public Map findOrderById(Integer id) throws Exception {
        Map map = orderDao.findOrderById(id);
        if (map!=null){
            Date orderDate = (Date) map.get("orderDate");
            //处理日期格式
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
